# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=headers
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgdesc="MinGW-w64 headers for Windows"
pkgver=8.0.0.6001.98dad1fe
pkgrel=1
_commit='98dad1fefed2a07ade0b37ca6c610ad66739631a'
arch=('any')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
makedepends=('git' "${MINGW_PACKAGE_PREFIX}-tools")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('!strip' '!libtool' '!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit"
        0001-Allow-to-use-bessel-and-complex-functions-without-un.patch
        0002-DirectX-9-fixes-for-VLC.patch)
sha256sums=('SKIP'
            'd641257f7e1469aff89adc33e57702b75fe9667ca035978f890eae1020b6814c'
            '09b1c7b62f666a07609af57e10c2b0ad815b78356f4b0f1fb6d827a1109a0ec7')

pkgver() {
  cd "${srcdir}/mingw-w64"
  local _major=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MAJOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MINOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _rev=0
  printf "%s.%s.%s.%s.%s" ${_major} ${_minor} ${_rev} "$(git rev-list --count $_commit)" "$(git rev-parse --short $_commit)"
}

prepare() {
  cd ${srcdir}/mingw-w64

  git am --committer-date-is-author-date "${srcdir}/0001-Allow-to-use-bessel-and-complex-functions-without-un.patch"
  git am --committer-date-is-author-date "${srcdir}/0002-DirectX-9-fixes-for-VLC.patch"



  cd ${srcdir}/mingw-w64/mingw-w64-headers
  touch include/windows.*.h include/wincrypt.h include/prsht.h
}

build() {
  msg "Configuring ${MINGW_CHOST} headers"
  [[ -d ${srcdir}/headers-${MINGW_CHOST} ]] && rm -rf ${srcdir}/headers-${MINGW_CHOST}
  mkdir -p ${srcdir}/headers-${MINGW_CHOST} && cd ${srcdir}/headers-${MINGW_CHOST}
  ${srcdir}/mingw-w64/mingw-w64-headers/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX}/${MINGW_CHOST} \
    --enable-sdk=all \
    --with-default-win32-winnt=0x601 \
    --enable-idl \
    --without-widl
}

package() {
  msg "Installing ${MINGW_CHOST} headers"
  cd ${srcdir}/headers-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  rm ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/pthread_signal.h
  rm ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/pthread_time.h
  rm ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/pthread_unistd.h

  msg "Installing MinGW-w64 licenses"
  install -Dm644 ${srcdir}/mingw-w64/mingw-w64-headers/ddk/readme.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ddk-readme.txt
  install -Dm644 ${srcdir}/mingw-w64/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 ${srcdir}/mingw-w64/COPYING.MinGW-w64/COPYING.MinGW-w64.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64.txt
  install -Dm644 ${srcdir}/mingw-w64/COPYING.MinGW-w64-runtime/COPYING.MinGW-w64-runtime.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64-runtime.txt
}
